﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;

/// <summary>
///informationToBeenSend 的摘要说明
/// </summary>
public class informationToBeenSend
{
    public string information;
    public string checkCode;
    public string lastTime;

    public informationToBeenSend(string username,string password)
    {
        string dirctory = HttpContext.Current.Request.PhysicalApplicationPath + "userdata//" + username;
        if (!Directory.Exists(dirctory))
        {
            information = "result=nouser;";
        }
        else
        {
            user userInstance = new user(HttpContext.Current.Request.PhysicalApplicationPath + "userdata//"+username + @"\user.xml");//读取user信息
            if (userInstance.Ps == password)//检验密码
            {
                DateTime old;
                if (userInstance.Lt == "")
                {
                    old = DateTime.MinValue;
                }
                else
                {
                    old = Convert.ToDateTime(userInstance.Lt);
                }
                if (userInstance.Lt == "" || old.AddDays(30) < DateTime.Now)//if the check code is not or expire, generate a new check code.
                {
                    checkCode = RandomString(10);
                    lastTime = DateTime.Now.ToShortDateString();
                }
                else
                {
                    checkCode = userInstance.Cc;
                    lastTime = userInstance.Lt;
                }
                information = "result=verified;username=" + username + ";checkcode="+checkCode+";";
            }
            else
            {
                information = "result=nouser;";
            }
        }        
    }

    private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));                 
            builder.Append(ch);
        }

        return builder.ToString();
    }
}